<?php
namespace app\admin\controller;

use app\common\model\ManagerCompany as ManagerCompanyModel;
use app\common\model\Company as CompanyModel;
use app\common\model\ManagerDuty as ManagerDutyModel;
use app\common\model\Manager as ManagerModel;
use app\common\model\ManagerPassword as ManagerPasswordModel;
use app\common\model\ManagerJob as ManagerJobModel;
use app\common\model\Renzhensiyuan as RenzhensiyuanModel;
use app\common\model\Member as MemberModel;
use think\Db;
/**
 * 法师寺院控制器
 * Class ManagerCompany
 * @package app\index\controller
 */
class ManagerCompany extends Base
{
    /**
     * [getIndex  查看]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new ManagerCompanyModel;
        $cmodel = new CompanyModel;
        $mmodel = new ManagerDutyModel;
        $jmodel = new ManagerJobModel;
        $map = [];
        $user = $this->user;
        try{
            $map['status'] = 1;
            $map['mid'] = $param['mid'];
            $list = $model->getAllData($map);
            foreach($list as &$v){
                $company = $cmodel->findData($v['comid'],'id','comname');
                $v['comname'] = $company['comname'];
                if($v['dutyid'] == 0){
                    $v['duty'] = '管理组';
                    $v['dutyid'] = 2;
                }else{
                    $duty = $mmodel->findData($v['dutyid'],'id','duty');
                    $v['duty'] = $duty['duty'];
                }
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if(!empty($v['jobid'])){
                    $job = $jmodel->findData($v['jobid'],'id','job');
                    $v['job'] = $job['job'];
                }
            }
        }catch(\Exception $e){
            return admin_json(-1, '查看法师关联寺院【'. $param['mid'].'】获取成功'.$e->getMessage());
        }
        writelog( '查看法师关联寺院【' . $param['mid']. '】获取成功',1,$user);
        return admin_json(1, '获取成功',$list);
    }
    /**
     * [postAdd  添加]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new ManagerCompanyModel;
        $mmodel = new ManagerModel;
        $mpmodel = new ManagerPasswordModel;
        $rmodel = new RenzhensiyuanModel;
        $memodel = new MemberModel;
        $user = $this->user;
        try{
            if(!empty($param['uid'])){
                $member = $memodel->findData($param['uid'],'id','username,tel,identity');
                $managerdata = $mmodel->findData($member['tel'],'tel');
                if(empty($managerdata)){
                    $param['mid'] = $mmodel->addData(['tel'=>$member['tel'],'username'=>$member['username'],'addtime'=>time()]);
                }else{
                    $param['mid'] = $managerdata['id'];
                }
            }
            $manager = $mmodel->findData($param['mid'],'id','comid,tel');
            $param['addtime'] = time();
            if(empty($param['dutyid'])){
                $param['dutyid'] = 1;
            }else{
                $pass = getRandom(12);
                $data = array(
                    'comid' => $param['comid'],
                    'mid' => $param['mid'],
                    'password' => md5(md5($pass) . config('admin.manager_password_halt')),
                    'pass' => $pass,
                    'tel' => $manager['tel'],
                    'jobid' => $param['jobid'],
                    'dutyid' => $param['dutyid'],
                    'states' => 1
                );
                if($param['dutyid'] == 1){
                    $data['states'] = -1;
                }else{
                    $data['states'] = 1;
                }
                $mpass = $mpmodel->findOne(['comid'=>$param['comid'],'mid'=>$param['mid']]);
                if(!empty($mpass)){
                    $data['id'] = $mpass['id'];
                    $model->editData($data);
                }else{
                    $data['addtime'] = time();
                    Db::name('manager_password')->insert($data);
                }
            }
            $mangercom = $model->findOne(['comid'=>$param['comid'],'mid'=>$param['mid']]);
            if(!empty($mangercom)){
                $id = $model->editData(['id'=>$mangercom['id'],'dutyid'=>$param['dutyid'],'status'=>1,'jobid'=>$param['jobid']]);
            }else{
                $id = $model->addData($param);
            }

            if(empty($manager['comid'])){
                $mmodel->editData(['id'=>$param['mid'],'comid'=>$param['comid']]);
            }
            if(!empty($param['renzheng'])){
                $rmodel->editData(['id'=>$param['rzid'],'status'=>1]);
            }
        }catch(\Exception $e){
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        writelog( '法师关联寺院【' . $id. '】添加成功',1,$user);
        return admin_json(1, '添加成功');
    }
    /**
     * [postEdit  修改]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $model = new ManagerCompanyModel;
        $user = $this->user;
        try{
            $id = $model->editData($param);
        }catch(\Exception $e){
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        writelog( '法师关联寺院【' . $id. '】修改成功',1,$user);
        return admin_json(1, '修改成功');
    }
    /**
     * [getDel  删除]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getDel()
    {
        $param = input('param.');
        $model = new ManagerCompanyModel;
        $user = $this->user;
        try{
            $model->deleteFalseData(['id'=>$param['id']],['status'=>-1]);
        }catch(\Exception $e){
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        writelog( '法师关联寺院【' . $param['id']. '】删除成功',1,$user);
        return admin_json(1, '删除成功');
    }
    /**
     * [getDel  删除]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAlladd()
    {
        $param = input('param.');
        $model = new ManagerCompanyModel;
        $mmodel = new ManagerModel;
        $memodel = new MemberModel;
        $mpmodel = new ManagerPasswordModel;
        $user = $this->user;
        try{
            $id = '';
            foreach($param['all'] as &$v){
                $member = $memodel->findData($v['mid'],'id','tel,username');
                $manager = $mmodel->findData($member['tel'],'tel','id,comid,tel,states');
                $v['addtime'] = time();
                if(empty($manager)){
                    $mid = $mmodel->addData(['tel'=>$member['tel'],'username'=>$member['username'],'addtime'=>time()]);
                }else{
                    if($manager['states'] == 3){
                        $mmodel->editData(['id'=>$manager['id'],'tel'=>$member['tel'],'username'=>$member['username'],'states'=>1]);
                    }
                    $mid = $manager['id'];
                }
                if(empty($v['dutyid'])){
                    $v['dutyid'] = 1;
                }else{
                    if($v['dutyid'] != 1){
                        $mpass = $mpmodel->findOne(['comid'=>$v['comid'],'mid'=>$v['mid']]);
                        $pass = getRandom(12);
                        $data = array(
                            'comid' => $v['comid'],
                            'mid' => $v['mid'],
                            'password' => md5(md5($pass) . config('admin.manager_password_halt')),
                            'pass' => $pass,
                            'tel' => $member['tel'],
                            'dutyid' => $v['dutyid']
                        );
                        if(!empty($mpass)){
                            $data['id'] = $mpass['id'];
                            $mpmodel->editData($data);
                        }else{
                            $data['addtime'] = time();
                            $mpmodel->addData($data);
                        }
                    }
                }
                $mangercom = $model->findOne(['comid'=>$v['comid'],'mid'=>$mid]);
                if(!empty($mangercom)){
                    $id = $model->editData(['id'=>$mangercom['id'],'dutyid'=>$v['dutyid'],'status'=>1,'jobid'=>$v['jobid']]);
                }else{
                    $v['mid'] = $mid;
                    $id = Db::table('app_manager_company')->insert($v);
                }

                if(empty($manager['comid'])) {
                    $mmodel->editData(['id' => $mid, 'comid' => $v['comid']]);
                }
            }
        }catch(\Exception $e){
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        writelog( '多个法师关联寺院【' . $id. '】添加成功',1,$user);
        return admin_json(1, '添加成功');
    }
}